package woaini.wind.bot.rasa;

import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import woaini.wind.bot.rasa.base.ActionExecutor;
import woaini.wind.bot.rasa.base.exception.ActionNotFoundException;
import woaini.wind.bot.rasa.base.exception.RejectExecuteException;
import woaini.wind.bot.rasa.base.rest.ActionCall;
import woaini.wind.bot.rasa.base.rest.ActionName;
import woaini.wind.bot.rasa.base.rest.ActionResponse;
import woaini.wind.bot.rasa.base.rest.StatusResponse;

import java.util.List;
import java.util.stream.Collectors;

/**
 * rasa 动作服务器回调相关
 * @see RasaActionHook
 * @author yefeng {@date 2024-10-29 17:41:49}
 */
@RestController
@RequestMapping("/rasa/action/")
public class RasaActionHook {

  @Resource
  private ActionExecutor actionExecutor;



  @GetMapping("/health")
  public StatusResponse health() {
    return new StatusResponse("ok");
  }

  @PostMapping("/webhook")
  public ActionResponse webhook(@RequestBody ActionCall actionCall) throws ActionNotFoundException, RejectExecuteException, JsonProcessingException {
    return actionExecutor.run(actionCall);
  }

  @GetMapping("/actions")
  public List<ActionName> actions() {
    return actionExecutor.getRegisteredActions()
      .stream()
      .map(ActionName::new)
      .collect(Collectors.toList());
  }
}
